<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script>
        //迭代器的使用可以极大的简化数据操作，于是ES6也向js中添加了迭代器特性
        function makeIterator(arr){
            let nextIndex=0;
            return {
                next:function(){
                    if(nextIndex<arr.length){
                        let temp=arr[nextIndex];//arr[0]  arr[1]  arr[2] arr[3]  arr[4]  arr[5]
                        nextIndex++;//1  2   3  4  5  6
                        return {value:temp,done:false};
                    }else{
                        return {value:undefined,done:true};
                    }
                }
            };
        }

        let arrs=[111,222,333,444,555];
        // let arrsIt=makeIterator(arrs);
        // let res1=arrsIt.next()
        // console.log(res1);
        // let res2=arrsIt.next()
        // console.log(res2);
        // let res3=arrsIt.next()
        // console.log(res3);
        // let res4=arrsIt.next()
        // console.log(res4);
        // let res5=arrsIt.next()
        // console.log(res5);
        // let res6=arrsIt.next()
        // console.log(res6);

        let arrsIt=makeIterator(arrs);
        do{
            var {value,done}=arrsIt.next();
            if(done==false){
                console.log(value);
            }
        }while(!done);

        var arrays=["aa","bb","cc","dd"];
        console.log(arrays);
        console.log("ES6为数组内置的迭代器");
        let arraysIt=arrays[Symbol.iterator]();
        console.log(arraysIt.next());
        console.log(arraysIt.next());
        console.log(arraysIt.next());
        console.log(arraysIt.next());
        console.log(arraysIt.next());

        //一个对象如果要具备可被for...of循环调用的 Iterator 接口，就必须在Symbol.iterator的属性上部署遍历器生成方法
        for(var v of arrays){
            console.log(v);
        }
    </script>
</head>
<body>
    
</body>
</html>